package com.shuai.api_platform.service.impl;

import cn.hutool.json.JSONUtil;
import com.shuai.api_platform.model.dto.BussModel;
import com.shuai.api_platform.model.dto.WeatherModel;
import com.shuai.api_platform.model.request.BussRequest;
import com.shuai.api_platform.service.ThirdPlatformsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ThirdPlatformsServiceImpl implements ThirdPlatformsService {

    @Autowired
    private RestTemplate restTemplate;


    @Value("${weather-service.API-KEY}")
    private String API_KEY;
    private final String WEATHER_REQUEST_URL = "https://restapi.amap.com/v3/weather/weatherInfo";
    private final String BUSS_REQUEST_URL = "https://restapi.amap.com/v3/bus/stopid ";

    /**
     * 根据城市编码获取当前的天气信息
     */
    public WeatherModel getWeatherInfo(String adCode) {
        String requestURLFinal = WEATHER_REQUEST_URL + "?key=" + API_KEY + "&city=" + adCode;
        String response = restTemplate.getForObject(requestURLFinal, String.class);
        return JSONUtil.toBean(response, WeatherModel.class);
    }

    @Override
    public WeatherModel getWeatherInfoPredict(String adCode) {
        String requestURLFinal = WEATHER_REQUEST_URL + "?key=" + API_KEY + "&city=" + adCode + "&extensions=all";
        String response = restTemplate.getForObject(requestURLFinal, String.class);
        return JSONUtil.toBean(response, WeatherModel.class);
    }

    @Override
    public BussModel getBussInfo(BussRequest request) {
        String requestURLFinal = BUSS_REQUEST_URL + "?key=" + API_KEY + "&id=" + request.getId();
        String response = restTemplate.getForObject(requestURLFinal, String.class);
        return JSONUtil.toBean(response, BussModel.class);
    }
}
